package com.ecoolex.hotel.saas.core.manager;

import com.ecoolex.framework.common.util.Check;
import com.ecoolex.hotel.saas.core.dao.StoreOrderConfigDAO;
import com.ecoolex.hotel.saas.entity.StoreOrderConfig;
import com.ecoolex.hotel.saas.entity.StoreOrderConfig.Column;
import com.ecoolex.hotel.saas.pojo.EditStoreOrderConfigDTO;

import java.time.Instant;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StoreOrderConfigManager {
	
    @Autowired
    private StoreOrderConfigDAO dao;
    
    public StoreOrderConfig getStoreOrderConfig(Integer storeId) {
		return dao.selectByPrimaryKey(storeId);
	}

	public StoreOrderConfig editStoreOrderConfig(EditStoreOrderConfigDTO edit, String updateUser){
		StoreOrderConfig config = getStoreOrderConfig(edit.getStoreId());
		if(Check.notNull(config)) {
			config.setAutoCancelTime(edit.getAutoCancelTime());
			config.setAutoConfirmTime(edit.getAutoConfirmTime());
			config.setUpdateUser(updateUser);
			config.setUpdateTime(Date.from(Instant.now()));
			dao.updateByPrimaryKeySelective(config, Column.excludes(Column.deleted,Column.createUser,Column.createTime));
		}else {
			config = new StoreOrderConfig();
			config.setStoreId(edit.getStoreId());
			config.setAutoCancelTime(edit.getAutoCancelTime());
			config.setAutoConfirmTime(edit.getAutoConfirmTime());
			config.setDeleted(StoreOrderConfig.NOT_DELETED);
			config.setCreateUser(updateUser);
			config.setCreateTime(Date.from(Instant.now()));
			config.setUpdateUser(updateUser);
			config.setUpdateTime(Date.from(Instant.now()));
			dao.insert(config);
		}
		return config;
	}
}